package sri.proiect.index;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import static sri.proiect.utils.DBColumn.*;

import sri.proiect.utils.DatabaseManager;


public class DatabaseIndexManager {
	
    //the directory that is used to store lucene index
    private final String indexDir = "c:\\indexDir";
    
	public boolean ifIndexExist(){
        File directory = new File(indexDir);
        if(0 < directory.listFiles().length){
        	return true;
        }else{
        	return false;
        }
    }
	
	public boolean createIndex() throws IOException{
    	if(true == ifIndexExist()){
    	    return true;	
    	}

    	Directory fsDirectory = FSDirectory.open(new File(indexDir));
    	Analyzer  analyzer    = new StandardAnalyzer(Version.LUCENE_CURRENT);
    	IndexWriter indexWriter = new IndexWriter(fsDirectory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);

    	
    	try {
			indexDatabase(indexWriter);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			indexWriter.optimize();
	    	indexWriter.close();
		}
    	
    	return true;
    	
    }
	private void indexDatabase(IndexWriter indexWriter) throws SQLException, IOException {

		Statement statement = DatabaseManager.getInstance().getConnection().createStatement();
		ResultSet rs = statement.executeQuery("select * from hotels");
		
		while(rs.next()) 
		{
			Document document = new Document();
			StringBuilder sb = new StringBuilder();
			
			processField(Nume_Hotel.getName(), rs, document, sb);
			processField(Judet.getName(), rs, document, sb);
			processField(Link.getName(), rs, document, sb);
			processField(Detalii.getName(), rs, document, sb);
			processField(Telefon.getName(), rs, document, sb);
			processField(Email.getName(), rs, document, sb);
			processField(NrStele.getName(), rs, document, sb);
			processField(Oras.getName(), rs, document, sb);
			processField(Adresa.getName(), rs, document, sb);
			processField(Website.getName(), rs, document, sb);
			
			document.add(new Field(FullDescription.getName(), sb.toString(), Store.YES, Index.ANALYZED));
			
			indexWriter.addDocument(document);
		}
	}

	private void processField(String fieldName, ResultSet rs, Document document, StringBuilder sb) throws SQLException {
		String fieldValue = rs.getString(fieldName);
		if (fieldValue != null) {
			document.add(new Field(fieldName, fieldValue, Store.YES, Index.ANALYZED));
			sb.append(fieldValue).append(" ");
		}
	}
	
	public String getIndexDir(){
    	return this.indexDir;
    }
	
}
